﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// std headers

// qt headers

// gcmp headers

// custom headers
#include "WidgetUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    WidgetUtils::WidgetUtils()
    {

    }

    WidgetUtils::~WidgetUtils()
    {

    }

    QWidget * WidgetUtils::GetRootWidget(QWidget * childWidget)
    {
        return WidgetUtils::GetAncestorWidget(childWidget, [=](QWidget *ancestorWidget) {
            return ancestorWidget != nullptr && ((ancestorWidget->windowFlags() & Qt::Dialog) || ancestorWidget->parentWidget() == nullptr);
        });
    }

    QPoint WidgetUtils::MapToAncestor(QWidget *widget, QWidget *ancestorWidget, const QPoint &pos)
    {
        if (!widget)
        {
            return pos;
        }

        if (!ancestorWidget)
        {
            return pos;
        }

        if (!WidgetUtils::IsInChain(widget, ancestorWidget))
        {
            return pos;
        }
                
        auto parent = widget->parentWidget();
        if (!parent)
        {
            return pos;
        }

        auto posMappedToParent = widget->mapTo(parent, pos);
        if (parent == ancestorWidget)
        {
            return posMappedToParent;
        }

        return WidgetUtils::MapToAncestor(parent, ancestorWidget, posMappedToParent);
    }

    bool WidgetUtils::IsInChain(QWidget * widget, QWidget * anotherWidget, QWidget * ancestorWidget, QWidget * offspringWidget)
    {
        if (WidgetUtils::GetAncestorWidget(widget, [=](QWidget *wgt) {return wgt == anotherWidget; }) != nullptr)
        {
            ancestorWidget = anotherWidget;
            offspringWidget = widget;
            return true;
        }

        if (WidgetUtils::GetAncestorWidget(anotherWidget, [=](QWidget *wgt) {return wgt == widget; }) != nullptr)
        {
            ancestorWidget = widget;
            offspringWidget = anotherWidget;
            return true;
        }

        return false;
    }

    bool WidgetUtils::IsInChain(QWidget * assertedOffspringWidget, QWidget * assertedAncestorWidget)
    {
        return WidgetUtils::GetAncestorWidget(assertedOffspringWidget, [=](QWidget *wgt) {return wgt == assertedAncestorWidget; }) != nullptr;
    }

}